package com.org.cys.service.impl.system;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.org.cys.controller.system.AdminController;
import com.org.cys.dao.base.IGeneralDao;
import com.org.cys.pojo.base.Page;
import com.org.cys.service.system.PrivilegeRoleService;
import com.org.cys.util.PageData;
import com.org.cys.util.StringUtil;


/**
 *	t_system_privilege_role Service实现类
 *	@author caiyushen
 */
@Service("privilegeRoleServiceImpl")
public class PrivilegeRoleServiceImpl implements PrivilegeRoleService {
  
  @Resource(name="generalDao")
  private IGeneralDao generalDao;
  
  public static String XMLMapper = "PrivilegeRoleMapper";
  
  private static Logger logger = Logger.getLogger(PrivilegeRoleServiceImpl.class);
  
 /**
  * 保存PrivilegeRole
  * @param PageData
  * @throws Exception
  * */
  @Override
  public void save(PageData pd) throws Exception{
	  generalDao.save(XMLMapper+".save",pd);
  }
  
 /**
   * 根据privilegeId批量插入PrivilegeRole
   * @param privilegeId 权限主键
   * @param dataList 数据集
   * */
  @Override
  public void saveBatchByPrivilegeId(List<PageData> dataList,Long privilegeId) throws Exception{
	  deleteByPrivilegeId(privilegeId);//先删除之前关联数据再重新添加新的关联数据
	  generalDao.batchSave(XMLMapper+".saveBatch", dataList);
  }
  
 /**
   * 根据roleId批量插入PrivilegeRole
   * @param roleId 角色主键
   * @param dataList 数据集
   * */
  @Override
  public void saveBatchByRoleId(List<PageData> dataList,Long roleId) throws Exception{
	  deleteByRoleId(roleId);//先删除之前关联数据再重新添加新的关联数据
	  generalDao.batchSave(XMLMapper+".saveBatch", dataList);
  }
  
 /**
  * 根据privilegeId获取PrivilegeRole
  * @param privilegeId 权限主键
  * @throws Exception 
  * */
  @Override
  public List<PageData> getListByPrivilegeId(Long privilegeId) throws Exception{
	  return (List<PageData>)generalDao.findForList(XMLMapper+".getListByPrivilegeId", privilegeId);
  }
  
 /**
  * 根据roleId获取PrivilegeRole
  * @param roleId 角色主键
  * @throws Exception 
  * */
  @Override
  public List<PageData> getListByRoleId(Long roleId) throws Exception{
	  return (List<PageData>)generalDao.findForList(XMLMapper+".getListByRoleId", roleId);
  }
  
 /**
   * 根据privilegeId删除PrivilegeRole
   * @param privilegeId 权限主键
   * @throws Exception  
   * */
  public void deleteByPrivilegeId(Long privilegeId) throws Exception{
	  generalDao.delete(XMLMapper+".deleteByPrivilegeId",privilegeId);
  }

 /**
   * 根据roleId删除PrivilegeRole
   * @param roleId 角色主键
   * @throws Exception  
   * */
  public void deleteByRoleId(Long roleId) throws Exception{
	  generalDao.delete(XMLMapper+".deleteByRoleId",roleId);
  }

  /**
   * 根据roleId获取权限数据
   * @param roleId 角色主键
   * @throws Exception 
   * */
  public List<PageData> getPrivilegeListByRoleIdAndType(Long roleId,Integer type) throws Exception{
	  List<PageData> dataList = new ArrayList<>();
	  if(StringUtil.checkObj(roleId) && StringUtil.checkObj(type)){
		  PageData pd = new PageData();
		  pd.put("roleId",roleId);
		  pd.put("type", type);
		  dataList = (List<PageData>)generalDao.findForList(XMLMapper+".getPrivilegeListByRoleIdAndType", pd);
	  }
	  logger.error("method getPrivilegeListByRoleIdAndType is error to param --> roleId:"+roleId+"type:"+type);
	  return dataList;
	  
  }
  
}